package com.yhxd.modular.news.service;

import com.querydsl.core.QueryResults;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.yhxd.modular.news.dao.AllInfoDao;
import com.yhxd.modular.news.dao.AttachmentDao;
import com.yhxd.modular.news.entity.Attachment;
import com.yhxd.modular.news.entity.Classify;
import com.yhxd.modular.news.entity.QAttachment;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@Service
public class AttachmentService {

    @Autowired
    private JPAQueryFactory factory;

    @Autowired
    private AttachmentDao attachmentDao;

    public List<Attachment> findAttachment(Long newsId) {
        QAttachment qAttachment = QAttachment.attachment;

        Predicate where = qAttachment.id.isNotNull();
        if (ObjectUtils.isNotEmpty(newsId)){      //分类id
            where = ExpressionUtils.and(where, qAttachment.newsId.eq(newsId));
        }
        QueryResults<Attachment> attachmentList = this.factory
                .selectFrom(qAttachment)
                .where(where)
                .fetchResults();
        return attachmentList.getResults();
    }
}
